In [19]:
import threading
from collections import deque
from IPython.display import display
import ipywidgets as widgets
import time
import websocket
from rsplib.processing import RSPSource
import json, requests

Out Stream


In [22]:
output_sream = requests.get('http://csparql:8182/queries/givemeall/observers/default').json()
output_sream['sld:streamLocation']


Out[22]:
'ws://csparql:8283/givemeall/results'

In [23]:
buffer = deque([], 10)

In [24]:
def on_message(ws, message):
    buffer.append(message)

def on_error(ws, error):
    print(error)

def on_close(ws):
    print("### closed ###")

def on_open(ws):
    def run(*args):
        for i in range(3):
            time.sleep(1)
            ws.send("Hello %d" % i)
        time.sleep(1)
        ws.close()
        print("thread terminating...")


def proc(on_open, on_error, on_message, on_close):
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp(output_sream['sld:streamLocation'],
                              on_message = on_message,
                              on_error = on_error,
                              on_close = on_close)
    ws.on_open = on_open
    ws.run_forever()

thread = threading.Thread(target=proc, args=(on_open, on_error, on_message, on_close,))
thread.start()


--- request header ---
GET /givemeall/results HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: csparql:8283
Origin: http://csparql:8283
Sec-WebSocket-Key: g7jklzNURZBFusNR9eCSOg==
Sec-WebSocket-Version: 13


-----------------------
--- response header ---
HTTP/1.1 101 Web Socket Protocol Handshake
Connection: Upgrade
Sec-WebSocket-Accept: m7oWn1b0o36Hv8SV/YDeXCcLdvg=
Upgrade: websocket
-----------------------

Visualizing a data example


In [32]:
json.loads(buffer[0])


Out[32]:
{'@context': {'eventTime': {'@id': 'http://www.streamreasoning.org/sld#eventTime',
   '@type': 'http://www.w3.org/2001/XMLSchema#dateTime'},
  'generatedAtTime': {'@id': 'http://www.w3.org/ns/prov#generatedAtTime',
   '@type': 'http://www.w3.org/2001/XMLSchema#dateTime'}},
 '@graph': [{'@id': 'http://aarhustrafficdata158505:4001/20783861-AS6ca7123a780eae27f4f0ee414625f850',
   'eventTime': 'http://www.w3.org/2001/XMLSchema#dateTime',
   'generatedAtTime': 'http://www.w3.org/2001/XMLSchema#dateTime'},
  {'@id': 'http://aarhustrafficdata158505:4001/20783861-CLd06db7595eb4d0cdf0b3abcb74362460',
   'eventTime': 'http://www.w3.org/2001/XMLSchema#dateTime',
   'generatedAtTime': 'http://www.w3.org/2001/XMLSchema#dateTime'},
  {'@id': 'http://aarhustrafficdata158505:4001/20783861-ETdc627dc1e84991494428f94eea930dec',
   'eventTime': 'http://www.w3.org/2001/XMLSchema#dateTime',
   'generatedAtTime': 'http://www.w3.org/2001/XMLSchema#dateTime'},
  {'@id': 'http://aarhustrafficdata158505:4001/20783861-MTd5abb28f370584cebd981d48c9906f04',
   'eventTime': 'http://www.w3.org/2001/XMLSchema#dateTime',
   'generatedAtTime': 'http://www.w3.org/2001/XMLSchema#dateTime'},
  {'@id': 'http://aarhustrafficdata158505:4001/20783861-VC4c6ae56a13d3527ca2687c8be1711267',
   'eventTime': 'http://www.w3.org/2001/XMLSchema#dateTime',
   'generatedAtTime': 'http://www.w3.org/2001/XMLSchema#dateTime'}]}

Wait for it...data is changed


In [31]:
json.loads(buffer[0])


Out[31]:
{'@context': {'eventTime': {'@id': 'http://www.streamreasoning.org/sld#eventTime',
   '@type': 'http://www.w3.org/2001/XMLSchema#dateTime'},
  'generatedAtTime': {'@id': 'http://www.w3.org/ns/prov#generatedAtTime',
   '@type': 'http://www.w3.org/2001/XMLSchema#dateTime'}},
 '@graph': [{'@id': 'http://aarhustrafficdata182955:4000/20785030-AS406c048f1cea2a0967568eb810a06dab',
   'eventTime': 'http://www.w3.org/2001/XMLSchema#dateTime',
   'generatedAtTime': 'http://www.w3.org/2001/XMLSchema#dateTime'},
  {'@id': 'http://aarhustrafficdata182955:4000/20785030-CL47e43e8144920906d99fc2769c6b26ab',
   'eventTime': 'http://www.w3.org/2001/XMLSchema#dateTime',
   'generatedAtTime': 'http://www.w3.org/2001/XMLSchema#dateTime'},
  {'@id': 'http://aarhustrafficdata182955:4000/20785030-ET197a674051822b87cb47ee3d26cd91da',
   'eventTime': 'http://www.w3.org/2001/XMLSchema#dateTime',
   'generatedAtTime': 'http://www.w3.org/2001/XMLSchema#dateTime'},
  {'@id': 'http://aarhustrafficdata182955:4000/20785030-MT1954a6c0978bf5288c0592c2c1952411',
   'eventTime': 'http://www.w3.org/2001/XMLSchema#dateTime',
   'generatedAtTime': 'http://www.w3.org/2001/XMLSchema#dateTime'},
  {'@id': 'http://aarhustrafficdata182955:4000/20785030-VCa9f036e3cd0379a740c9d659fe5dcc89',
   'eventTime': 'http://www.w3.org/2001/XMLSchema#dateTime',
   'generatedAtTime': 'http://www.w3.org/2001/XMLSchema#dateTime'}]}

In [ ]: